package org.jeecg.modules.inspection.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.inspection.entity.ExceptionProcess;
import org.jeecg.modules.inspection.entity.PinspectionException;
import org.jeecg.modules.inspection.mapper.PinspectionExceptionMapper;
import org.jeecg.modules.inspection.service.IPinspectionExceptionService;
import org.jeecg.modules.system.entity.SysUserPosition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 巡检运行异常
 * @Author: jeecg-boot
 * @Date:   2019-09-26
 * @Version: V1.0
 */
@Service
public class PinspectionExceptionServiceImpl extends ServiceImpl<PinspectionExceptionMapper, PinspectionException> implements IPinspectionExceptionService {
    @Autowired
    private PinspectionExceptionMapper exceptionMapper;
    @Override
    public String selectOffice(String departId) {
        return exceptionMapper.selectOffice(departId);
    }

    @Override
    public List<SysUserPosition> selectAllConfigUser(List<String> userLists, String officeId, String userId) {
        return exceptionMapper.selectAllConfigUser(userLists,officeId,userId);
    }

    @Override
    public List<PinspectionException> selectExpection(List<String> itemIds) {
        return exceptionMapper.selectExpection(itemIds);
    }

    @Override
    public List<PinspectionException> selectDealExpection(String userId) {
        return exceptionMapper.selectDealExpection(userId);
    }

    @Override
    public PinspectionException selectExce(String pitemId,String areaId) {
        return exceptionMapper.selectExce(pitemId,areaId);
    }

    @Override
    public PinspectionException selectById(String id) {
        return exceptionMapper.selectById(id);
    }

    @Override
    public Integer updateStatus(PinspectionException pinspectionException) {
      return   exceptionMapper.updateStatus(pinspectionException);
    }

    @Override
    public List<PinspectionException> getAllExce(String userId,String officeId,String str) {
         //查询指派给该人员的异常任务
        List<PinspectionException> exceptionList = exceptionMapper.selectByUser(userId,officeId);
        //获取该管理处未处理的任务
        if (StringUtils.isNotEmpty(str)) {
            List<String> typeList = Arrays.asList(str.split(","));
            List<PinspectionException> exceptionList1 = exceptionMapper.selectAllExce(officeId, typeList);
            exceptionList.addAll(exceptionList1);
            Collections.sort(exceptionList, new Comparator<PinspectionException>(){
                public int compare(PinspectionException o1, PinspectionException o2) {
                    if(after(o1.getCreateTime(), o2.getCreateTime())){
                        return 1;
                    }
                    return 1;
                }
            });
        }
        return exceptionList;
    }
    public static boolean after(Date date1,Date date2){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        return c2.after(c1);
    }

    @Override
    public void insertDealContents(PinspectionException pinspectionException) {
        exceptionMapper.insertDealContents(pinspectionException);
    }

    @Override
    public void updateConfirm(String id) {
        exceptionMapper.updateConfirm(id);
    }

    @Override
    public Integer insertProcess(ExceptionProcess exceptionProcess) {
        return exceptionMapper.insertProcess(exceptionProcess);
    }

    @Override
    public void updateExce(List<String> trueItems) {
        exceptionMapper.updateExce(trueItems);
    }

    @Override
    public List<PinspectionException> queryByItemId(String itemId) {
        return exceptionMapper.queryByItemId(itemId);
    }

    @Override
    public List<PinspectionException> queryExceptionList(String processItemId) {
        return exceptionMapper.queryExceptionList(processItemId);
    }
}
